$(function () {
    // 获取商店详情
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var id = Common.fn.getUrlParam('id'),
        userRepresentId = Common.fn.getUrlParam('userRepresentId');
    if (id) {
        var result = Model.business.businessDetail({
            businessId: id
        });
        if (!sessionStorage.getItem('hmdj_stopError')) {
            sessionStorage.setItem('hmdj_stopError', true);
        } else {
            if (result.code) {
                Common.fn.formatOutput(result);
                loading.hide();
                return false;
            }
        }
        $('.portrait-img').attr('src', result.data[0].business.photo);
        $('.vendor-level').text('v' + result.data[0].business.grade);
        $('.nickname').text(result.data[0].business.nickname);
        $('.grade').text(result.data[0].business.credits);
        result.data[0].business.auth == true ? $('.auth').text('已认证') : $('.auth').text('未认证');
        $('.bond').text(result.data[0].business.bond);
        $('.tel-btn').attr('href', 'tel:' + result.data[0].business.phone);
        $('.attention-btn').attr('data-id', result.data[0].business.businessId);
        $('.chat-btn').attr('data-id', result.data[0].business.businessId);
        $('.chat-btn').attr('data-nickname', result.data[0].business.nickname);
        // 关注按钮改变
        if (result.data[0].business.attention == true) {
            // 已关注
            $('.attention-btn').text('已关注');
            $('.attention-btn').attr('data-state', 1);
        } else {
            $('.attention-btn').text('关注');
            $('.attention-btn').attr('data-state', 0);
        }
        // 遍历全部拍品
        for (var i = 0; i < result.data[0].goods.length; i++) {
            var item = '<a href="goods_detail.html?gid=' + result.data[0].goods[i].goodsId + '&bid=' + result.data[0].business.businessId + '" class="goods-flow-item goods-item"><img src="' + result.data[0].goods[i].picture + '"><h4>' + result.data[0].goods[i].content + '</h4><p class="price text-d81e06">￥' + result.data[0].goods[i].topPrice + '起</p></a>';
            $('.goods-list').append(item);
        }
    } else {
        weui.alert('未获取到店铺信息', function () {
            location.href = '../index.html';
        });
    }
    // 若该页面通过海报扫码进入，则需要进行关注操作
    if (userRepresentId) {
        var result = Model.represent.attentionByCode({
            userRepresentId: userRepresentId
        });
        // 这句话不能删，别问为什么，删了就会出错
        if (result.code) {
            // Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
        location.href = 'shop_detail.html?id=' + Common.fn.getUrlParam('id');
    }
    loading.hide();
});

// 关注按钮
$(document).on('click', '.attention-btn', function () {
    var id = $(this).attr('data-id'),
        state = $(this).attr('data-state');
    if (state == 1) {
        // 已关注
        weui.confirm(
            "真的要取消关注吗？",
            function () {
                // 取消关注
                result = Model.business.attendBusiness({
                    businessId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.href = 'shop_detail.html?id=' + Common.fn.getUrlParam('id');
                    return false;
                }
                location.href = 'shop_detail.html?id=' + Common.fn.getUrlParam('id');
            },
            function () {}
        );
    } else {
        // 关注店铺
        var loading = weui.loading('loading', {
            className: 'custom-classname'
        });
        result = Model.business.attendBusiness({
            businessId: id
        });
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
        loading.hide();
        location.href = 'shop_detail.html?id=' + Common.fn.getUrlParam('id');
    }
});

// 私信按钮
$(document).on('click', '.chat-btn', function () {
    var id = $(this).attr('data-id'),
        nickname = $(this).attr('data-nickname');
    location.href = 'chat.html?cid=' + id + '&nickname=' + nickname;
});